package org.wlw.teacher.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.wlw.teacher.service.IDepartmentService;

import javax.servlet.http.HttpServletRequest;

//相当于@Componment
@Controller
public class HelloController {
    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/hello")
    public ModelAndView hello(ModelAndView modelAndView, HttpServletRequest request) {
//        modelAndView.setViewName("/WEB-INF/jsp/hello.jsp");
        //相当于request.setAttribute("name","admin");
        int id = 1;
        try {
            id = Integer.parseInt(request.getParameter("id"));
        } catch (NumberFormatException e) {
        }
        modelAndView.addObject("dep", departmentService.load(id));
        modelAndView.setViewName("hello");
        return modelAndView;
    }

    //实际的开发过程中，使用的是下面这种方案
//    @RequestMapping("/hello1")
    @GetMapping("/hello1") //只满足GET请求
    public String hello1(Model model, HttpServletRequest request) {
        int id = 1;
        try {
            id = Integer.parseInt(request.getParameter("id"));
        } catch (NumberFormatException e) {
        }
        model.addAttribute("dep", departmentService.load(id));
        return "hello";
    }
}
